package com.ly.honor.util;

import cn.hutool.core.date.DateUtil;
import com.ly.honor.exception.HonorException;
import io.minio.*;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author 李某人
 * @Date 2023/1/25 18:35
 * @Version 1.0
 */
@Component
@Slf4j
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;

    public void createBucket(String bucketName){
        try {
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        } catch (Exception e) {
            log.error("创建桶失败:{}", e);
            throw new HonorException("创建桶失败");
        }
    }

    //检查存储桶是否存在。
    public boolean bucketExists(String bucketName){
        try {
            // 检查'my-bucketname'是否存在。
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (found) {
                System.out.println(bucketName + " exists");
            } else {
                System.out.println(bucketName + " does not exist");
            }
            return found;
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
        return false;
    }

    public void removeBucket(String bucketName)throws Exception{
        try {
            // 删除之前先检查`my-bucket`是否存在。
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (found) {
                // 删除`my-bucketname`存储桶，注意，只有存储桶为空时才能删除成功。
                minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
                System.out.println(bucketName + " is removed successfully");
            } else {
                System.out.println(bucketName + " does not exist");
            }
        } catch(MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }

    /**
     * 上传 MultipartFile
     * @param file
     * @param bucketName
     * @param remoteName
     * @return
     * @throws Exception
     */
    public ObjectWriteResponse uploadFile(MultipartFile file, String bucketName,String remoteName) throws Exception {
        //判断文件是否为空
        if (null == file || 0 == file.getSize()) {
            throw new HonorException("上传文件不能为空");
        }
        return minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(remoteName)
                .stream(file.getInputStream(), file.getSize(), PutObjectArgs.MIN_MULTIPART_SIZE)
                .build());
    }

    public String getDatePath(){
        return DateUtil.format(DateUtil.date(),"yyyy/MM/dd") + "/";
    }

}
